<html>
<body>
<head>
<title></title>
<script type ="text/javascript">
var groups = <?php echo $count;?>;
function add_textbox(){
	//alert(groups);
	groups++;
	tester = "";
	var myString = "";
	
	for(i = 1; i <= groups; i++){
		
		value = i;
		//alert(value);
		test = "group"+value;
		if (i == groups){
			tester = "";
		}
		else{
			
			tester = document.getElementById(test).value;
			
		}
		//alert(tester);
		//alert(tester);
		
		myString += "Group "+value+" <input id ="+test+" type='text' name='group"+value+"' value="+tester+">"+"<br>";
		//alert(tester);
		
	}
	document.getElementById("optional_groups").innerHTML = myString;
}
function delete_textbox(){
	
	if(groups == 1){
	}
	else{
		groups--;
	}
	var myString = "";
	for(i = 1; i <= groups; i++){
		value = i;

		test = "group"+value;

		tester = document.getElementById(test).value;
			
		
		myString += "Group "+value+" <input id ="+test+" type='text' name='group"+value+"' value="+tester+">"+"<br>";
		
	}
	document.getElementById("optional_groups").innerHTML = myString;	
	
}


function convert(month){
	if (month == "jan"){
		return 1;
	}
	else if (month == "feb"){
		return 2;
	}
	else if (month == "mar"){
		return 3;
	}
	else if(month == "april"){
		return 4;
	}
	else if(month == "may"){
		return 5;
	}
	else if(month == "june"){
		return 6;
	}
	else if(month == "july"){
		return 7;
	}
	else if(month == "august"){
		return 8;
	}
	else if(month == "sept"){
		return 9;
	}
	else if(month == "oct"){
		return 10;
	}
	else if(month == "nov"){
		return 11;
	}
	else if (month == "dec"){
		return 12;
	}
}

function valid_date(){
	bm = document.getElementById("try");
	bd = document.getElementById("try2");
	by = document.getElementById("start_year");
	var begin_month = bm.options[bm.selectedIndex].value;
	var begin_day = bd.options[bd.selectedIndex].value;
	var begin_year = by.options[by.selectedIndex].value;
	result2 = true;
	begin_month = convert(begin_month);
	begin_day = Number(begin_day);
	begin_year = Number(begin_year);
	em = document.getElementById("end_try");
	ed = document.getElementById("end_try2");
	ey = document.getElementById("end_year");
	
	var end_month = em.options[em.selectedIndex].value;
	var end_day = ed.options[ed.selectedIndex].value;
	var end_year = ey.options[ey.selectedIndex].value;
	end_month = convert(end_month);
	end_day = Number(end_day);
	end_year = Number(end_year);
	
	//alert(begin_month);
	//alert(begin_day);
	//alert(begin_year);
	result = false;
	//alert(end_month);
	//alert(end_day);
	//alert(end_year);
	if (end_year == begin_year){
		if (end_month == begin_month){
			if (end_day >= begin_day){
				result = true;
			}
			else {
				
			}
		}
		else if (end_month < begin_month){
			
		}
		else{
			result = true;
		}
	}
	else if (end_year < begin_year){
		
	}
	else{
		result = true;
	}
	
	//id_index = 1;
	//alert("wooot");
	//alert(document.getElementById("group1").value);
	//alert(groups);
	check = document.getElementById("use_groups").checked;
	if (check == 1){
		for(i = 1; i <= groups; i++){
			id_ind = "group"+i;
			
			toCheck = document.getElementById(id_ind).value;
			//alert(toCheck);
			if(toCheck == null || toCheck == ""){
				result2 = false;
				break;
			}
			
		}
		
		
		if (result == true && result2 == true){
			return true;
		}
		else {
			if(result == false && result2 == true){
				alert('Invalid begin date -> end date combination');
			}
			else if (result2 == false && result == false) {
				alert('Invalid begin date -> end date combination');
				alert('groups must be filled out!');
			}
			else if (result2 == false && result == true){
				alert('groups must be filled out!');
			}
			return false;
		}	
	}
	else{
		result2 = true;
		if (result == true && result2 == true){
			return true;
		}
		else {
			if(result == false && result2 == true){
				alert('Invalid begin date -> end date combination');
			}
			else if (result2 == false && result == false) {
				alert('Invalid begin date -> end date combination');
				alert('groups must be filled out!');
			}
			else if (result2 == false && result == true){
				alert('groups must be filled out!');
			}
			return false;
		}	
	}

}
function over_all_check(){
	result = valid_date();
	valid = true;
	//alert("what");
	survey_name = document.getElementById("survey_name").value;
	//alert(survey_name);
	if (survey_name == "" || survey_name == null){
		//alert("Survey name must be filled out!")
		valid = false;
	}
	if (valid == false){
		//alert("wtf");
		//alert(valid);
		//alert(result);
		alert("Survey name must be filled out!");
		return false;
	}
	
	else{
		//alert("wth");
		//alert(valid);
		//alert(result);	
		return result;
	}

}






	function check(test,test2){
		var e = document.getElementById(test);
		var month = e.options[e.selectedIndex].value;
		//document.write(month);
		day = document.getElementById(test2);
		limit = day.options.length;
		if (month == "feb"){
			if (limit == 29){
				//do nothing
			}
			else{
				/*
				for(i = 28; i <= limit; i++){
					optn = document.createElement("OPTION");
					option.text = i;
					option.value = i;
					day.options.add(optn);
				}*/
				while (limit != 29){
					day.remove(limit-1);
					limit =  day.options.length;
					//alert(limit);
				}
			}
		}
		else if (month == "jan" || month == "mar" || month == "may" || month == "july" || month == "august" || month == "oct" || month == "dec"){
			if (limit == 31){//31 days
			}
			else {
				while(limit < 31){
					//alert(limit);
					optn = document.createElement("option");
					optn.text = limit+1;
					optn.value = limit+1;
					day.options.add(optn);
					limit = day.options.length;
				}
			}
		}
		else{
			if (limit == 30){
			}
			else if (limit > 30){
				day.remove(30);
			}
			else{
				while(limit < 30){
					//alert(limit);
					optn = document.createElement("option");
					optn.text = limit+1;
					optn.value = limit+1;
					day.options.add(optn);
					limit = day.options.length;			
				}
			}
		}
	}
</script>
</head>
<form method="POST" action='http://localhost/pollwatcher/index.php/user/update_survey_todb' onsubmit="return over_all_check()">
<?php
	$notif = $survey->row()->notif_sched;
	$begin_date = $survey->row()->begin_date;
	$begin_year = (int)substr($begin_date,0,4);
	$begin_month = (int)substr($begin_date,5,2);
	$begin_day = (int)substr($begin_date,8,2);

	$end_date = $survey->row()->end_date;
	$end_year = (int)substr($end_date,0,4);
	$end_month = (int)substr($end_date,5,2);
	$end_day = (int)substr($end_date,8,2);
	//echo $begin_date;
	/*
	echo $begin_year, "<br>";
	echo $begin_month, "<br>";
	echo $begin_day, "<br>";
	echo $end_date, "<br>";
	echo $end_month, "<br>";
	echo $end_year, "<br>";*/
	if ($req->num_rows == 0){
		$firstname = 0;
		$middlename = 0;
		$lastname = 0;
		$gender = 0;
		$birthday = 0;
		$location = 0;
	} 
	else{
		$firstname = $req->row()->firstname;
		$middlename = $req->row()->middlename;
		$lastname = $req->row()->lastname;
		$gender = $req->row()->gender;
		$birthday = $req->row()->birthday;
		$location = $req->row()->location;		
	}

?>
Survey name: <input id="survey_name" type="text" name="survey_name" value="<?php echo $survey->row()->title; ?>"  /><br/>
Description: <br/><textarea  name="description" cols=60 rows=6><?php echo $survey->row()->description; ?></textarea><br/>
Begin date: Month:<select id = "try" name="month" onchange="check('try','try2')">
<option value="jan" 
<?php
	if ($begin_month == 1){
		echo " SELECTED ";
	}
?> >January</option>
<option value="feb"   
<?php
	if ($begin_month == 2){
		echo " SELECTED ";
	}
?> >February</option>
<option value="mar"
<?php
	if ($begin_month == 3){
		echo " SELECTED ";
	}
?>
>March</option>
<option value="april"
<?php
	if ($begin_month == 4){
		echo " SELECTED ";
	}
?>  
 >April</option>
<option value="may"  
<?php
	if ($begin_month == 5){
		echo " SELECTED ";
	}
?>
>May</option>
<option value="june"   
<?php
	if ($begin_month == 6){
		echo " SELECTED ";
	}
?>
>June</option>
<option value="july"   
<?php
	if ($begin_month == 7){
		echo " SELECTED ";
	}
?>
>July</option>
<option value="august"   
<?php
	if ($begin_month == 8){
		echo " SELECTED ";
	}
?>
>August</option>
<option value="sept"   
<?php
	if ($begin_month == 9){
		echo " SELECTED ";
	}
?>
>September</option>
<option value="oct"   
<?php
	if ($begin_month == 10){
		echo " SELECTED ";
	}
?>
>October</option>
<option value="nov"  
<?php
	if ($begin_month == 11){
		echo " SELECTED ";
	}
?>
>November</option>
<option value="dec"   
<?php
	if ($begin_month == 12){
		echo " SELECTED ";
	}
?>
>December</option>
</select>
<?php
	//echo $begin_day;
?>
Day:<select id = "try2" name="day">

<?php
	for($i = 1; $i <= 31; $i++){
		echo "<option value='$i'";
		
		if ($begin_day == $i){
			echo " SELECTED ";
		}
		echo ">";
		echo $i;
		echo "</option>";
	}
?>

</select>

Year<select id="start_year" name="year">
<?php
		$year = (int)date('Y');
		for($i = $year; $i<($year+100); $i++){
			
			
			//echo "<option "."value='$i'".">".$i."</option>";
			echo "<option ";
			echo "value=$i";
			if ($begin_year == $i){
				echo " SELECTED ";
			}
			echo ">";
			echo $i;
			echo "</option>";
		}
?>
</select><br/>

End date: Month:<select id = "end_try" name="end_month" onchange="check('end_try','end_try2')">
<option value="jan" 
<?php
	if ($end_month == 1){
		echo " SELECTED ";
	}
?> >January</option>
<option value="feb"   
<?php
	if ($end_month == 2){
		echo " SELECTED ";
	}
?> >February</option>
<option value="mar"
<?php
	if ($end_month == 3){
		echo " SELECTED ";
	}
?>
>March</option>
<option value="april"
<?php
	if ($end_month == 4){
		echo " SELECTED ";
	}
?>  
 >April</option>
<option value="may"  
<?php
	if ($end_month == 5){
		echo " SELECTED ";
	}
?>
>May</option>
<option value="june"   
<?php
	if ($end_month == 6){
		echo " SELECTED ";
	}
?>
>June</option>
<option value="july"   
<?php
	if ($end_month == 7){
		echo " SELECTED ";
	}
?>
>July</option>
<option value="august"   
<?php
	if ($end_month == 8){
		echo " SELECTED ";
	}
?>
>August</option>
<option value="sept"   
<?php
	if ($end_month == 9){
		echo " SELECTED ";
	}
?>
>September</option>
<option value="oct"   
<?php
	if ($end_month == 10){
		echo " SELECTED ";
	}
?>
>October</option>
<option value="nov"  
<?php
	if ($end_month == 11){
		echo " SELECTED ";
	}
?>
>November</option>
<option value="dec"   
<?php
	if ($end_month == 12){
		echo " SELECTED ";
	}
?>
>December</option>
</select>

Day:<select id = "end_try2" name="end_day">
<?php
	for($i = 1; $i <= 31; $i++){
		echo "<option value='$i'";
		
		if ($end_day == $i){
			echo " SELECTED ";
		}
		echo ">";
		echo $i;
		echo "</option>";
	}
?>
</select>

Year<select id="end_year" name="end_year">
<?php
		$year = (int)date('Y');
		for($i = $year; $i<($year+100); $i++){
			//echo "<option "."value='$i'".">".$i."</option>";
			echo "<option ";
			echo "value=$i";

			if ($end_year == $i){
				echo " SELECTED ";
			}
			
			echo ">";
			echo $i;
			echo "</option>";
		}
?>
</select><br/>

<input type ="hidden" name="id" value = "<?php echo $id;?>"/><br/>

<?php
	if($answers == 0){
		echo "<input id='use_groups' name='checker' type='checkbox'"; 
	}
	
?>
<?php
	if ($has_groups){
		echo " CHECKED ";
	}
	else{
	}
	//Group 1 <input  type="text" name="group1"/>
	if ($answers == 0){
		echo "value='1'><label for='use_groups'> Use question groups</label>";
	}
	
?>
<div id="groups">
	
	<div id ="optional_groups">
		<?php
			if ($answers == 0){
				$i = 1;
				foreach($categories->result() as $category){
					$name = "group".$i;
					$value = $category->category_name;
					$value_id = $category->category_id;
					echo "<input class='check' type='checkbox' value=\"$value_id\" name='toDelete[]' > ";
					echo "Group $i <input  id=\"$name\" type='text' name=\"$name\" value=\"$value\">";
					$i++;
					
					echo "<br>";
				}
			}			
		?>
	</div>
	<button  id ="add_button" type="button" onclick="add_textbox()">Add Group</button>
	<button id="del_button" type="button">Delete Group</button>
	
</div>
<script type="text/javascript" src="<?php echo base_url(); ?>/public/js/jquery.js"></script>
	<script type="text/javascript">
	//alert("a");
	//$i = 0;
    $(document).ready(function(){
		i = 0;
       //$("#groups").css("display","none");
	   //alert("a");
       //$("#text_options_Text").css("display","none");
      // $("#choice_limit_div").css("display","none");
		$(".check").css("display","none");
		$("#add_button").hide("fast");
		$("#del_button").hide("fast");
		
		if($("#use_groups").is(":checked")){
			$("#groups").show("fast");
			$("#add_button").show("fast");
			$("#del_button").show("fast");				
		}
		else{
			$("#groups").hide("fast");
		}

		$("#add_button").click(function(){
			$("#del_button").hide("fast");
		});
		$("#del_button").click(function(){
			$("#add_button").hide("fast");
			if(i == 0){
				$(".check").show("fast");
				
				i = 1;
				//$("#del_button").html("Im done deleting");
			}
			else{
				i = 0;
				$(".check").hide("fast");
			}
			
			
		});
	 	$("#use_groups").click(function(){
			if($("#use_groups").is(":checked")){
				$("#groups").show("fast");
				$("#add_button").show("fast");
				$("#del_button").show("fast");				
			}
			else{
				$("#groups").hide("fast");
			}
	   });
	   
	   
	  
       }
       

    
    );
</script>
<br/>





Notification schedule:<select name="sched">

<option value="1" 
<?php
	if ($notif == 1){
		echo " SELECTED";
	}
?>>1 Day</option>
<option value="5" 
<?php
	if ($notif == 5){
		echo " SELECTED";
	}
?>
>5 Days</option>
<option value="10" 
<?php
	if ($notif == 10){
		echo " SELECTED";
	}
?>
>10 Days</option>
<option value="15"
<?php
	if ($notif == 15){
		echo " SELECTED";
	}
?>
>15 Days</option>
<option value="30"
<?php
	if ($notif == 30){
		echo " SELECTED";
	}
?>
>30 Days</option>
<option value="0" 
<?php
	if ($notif == 0){
		echo " SELECTED";
	}
?>
>no notification</option>
</select><br/>

Notification message:<br />

<input type="radio" id="template" name="mode" value="template"> Use an existing template</input><br/>
<input type="radio" id="custom" name="mode" value="custom"> Use a custom message</input><br/><br/>

<?php echo form_textarea(array('id' => 'body', 'name' => 'notification_message', 'cols' => '70', 'rows' => '10'), $survey->row()->notif_body); ?>

<div id="loaded" style="display:none">
Templates: <br/>
<?php foreach($templates as $template)
{
	echo "<input type=\"radio\" name=\"loaded_templates\" value=\"$template\"> Use this template</input><br/><textarea readonly cols=70 rows=10> $template</textarea><br/><br/>";	
}

?>

</div>
<br /><br />



Required Information from Respondents: <br/>
<input type="checkbox" id="chkFirstName" name="required[]" 
<?php 
	if ($firstname == 1){
		echo " checked = 'checked' ";
	}
?> value="firstname"><label for="chkFirstName"> Firstname</label><br/>
<input type="checkbox" id="chkMiddleName"  name="required[]"
<?php
	if ($middlename == 1){
		echo " checked = 'checked' ";
	}	
?> value="middlename"><label for="chkMiddleName"> Middlename</label><br/>
<input type="checkbox" id="chkLastName" name="required[]" 
<?php
	if ($lastname == 1){
		echo " checked = 'checked' ";
	}
?> value="lastname"><label for ="chkLastName"> Lastname</label><br/>
<input type="checkbox" id="chkGender" name="required[]"
<?php
	if ($gender == 1){
		echo " checked = 'checked' ";
	}
?> value="gender"><label for="chkGender"> Gender</label><br/>
<input type="checkbox" id="chkBirthday" name="required[]"
<?php
	if ($birthday == 1){
		echo " checked = 'checked' ";
	}
?>	 value="birthday"><label for="chkBirthday"> Birthday</label><br/>
<input type="checkbox" id="chkLocation" name="required[]"
<?php
	if ($location == 1){
		echo " checked = 'checked' ";
	}
?> value="location"><label for="chkLocation"> Location<label><br/>
 <input type="hidden" name="survey_id" value ="<?php echo $survey_id;?>">
<input type="submit" name="update" value="Update Survey!">

</body>
</html>

<script src="<?php echo base_url(); ?>public/js/jquery.js"></script>
<script type="text/javascript">
	
	$(document).ready(function(){
		$("#template").change(function(){
			$("#body").hide('fast');
			$("#loaded").show('fast');
			
		});
		
		$("#custom").change(function(){
			$("#loaded").hide('fast');
			$("#body").val('Insert a notification message');
			$("#body").show('fast');
		});
		
		$("input[name='loaded_templates']").change(function(){
				$("#body").val($("input[name='loaded_templates']:checked").val());
			});
	});
</script>

